package com.cuitrip.business.home.travel;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.cuitrip.business.home.travel.model.SelectInfo;
import com.cuitrip.service.R;
import com.cuitrip.util.track.TrackUtil;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragmentActivity;
import com.lab.logtrack.AppLog;

/**
 * Created by ak
 */
public class TravelTripListActivity extends CustomUiFragmentActivity {

    private SelectInfo selectInfo;
    private TravelTripListFragment fragment;

    public static void search(Activity activity, String title, String searchParams, String language, String from, String type) {
        Intent intent = new Intent(activity, TravelTripListActivity.class);
        if (!TextUtils.isEmpty(language)) {
            intent.putExtra(SelectInfo.LANGUAGE_KEY, language);
        }
        if (!TextUtils.isEmpty(title)) {
            intent.putExtra(SelectInfo.TITLE_KEY, title);
        }
        if (!TextUtils.isEmpty(searchParams)) {
            intent.putExtra(SelectInfo.SEARCH_PARAMS_KEY, searchParams);
        }
        if (!TextUtils.isEmpty(from)) {
            intent.putExtra(SelectInfo.KEY_FROM, from);
        } else {
            intent.putExtra(SelectInfo.KEY_FROM, SelectInfo.FROM_HOME_CARD);
        }
        if (!TextUtils.isEmpty(type)) {
            try {
                int typeInt = Integer.parseInt(type);
                intent.putExtra(SelectInfo.KEY_TYPE, typeInt);
            } catch (Exception e) {
                AppLog.e(e.getMessage());
            }
        }
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (getIntent() == null) {
            super.onCreate(savedInstanceState, R.layout.page_container_layout);
            return;
        }
        selectInfo = new SelectInfo();
        if (getIntent().hasExtra(SelectInfo.KEY_WORD_KEY)) {
            selectInfo.setKeyWords(getIntent().getStringExtra(SelectInfo.KEY_WORD_KEY));
            TrackUtil.trackScreenName("/search/results");
        }
        if (getIntent().hasExtra(SelectInfo.CITY_KEY)) {
            String cityAbbr = getIntent().getStringExtra(SelectInfo.CITY_KEY);
            selectInfo.setCityAbbr(cityAbbr);
            TrackUtil.trackScreenName("/%s/%s", "TW", cityAbbr);
        }
        if (getIntent().hasExtra(SelectInfo.KEY_FROM)) {
            String from = getIntent().getStringExtra(SelectInfo.KEY_FROM);
            selectInfo.setFrom(from);
        }
        if (getIntent().hasExtra(SelectInfo.TITLE_KEY)) {
            String title = getIntent().getStringExtra(SelectInfo.TITLE_KEY);
            selectInfo.setTitle(title);
        }
        if (getIntent().hasExtra(SelectInfo.SEARCH_PARAMS_KEY)) {
            String searchParams = getIntent().getStringExtra(SelectInfo.SEARCH_PARAMS_KEY);
            selectInfo.copySearchParams(searchParams);
        }
        if (getIntent().hasExtra(SelectInfo.KEY_TYPE)) {
            selectInfo.setType(getIntent().getIntExtra(SelectInfo.KEY_TYPE, 0));
        }
        super.onCreate(savedInstanceState, R.layout.page_container_layout);
    }

    public void initView() {
        fragment = TravelTripListFragment.newInstance(selectInfo);
        openFragment(fragment, R.id.container, TravelTripListFragment.class.getName());
    }

    @Override
    public boolean initCloseTopBar() {
        return true;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        return null;
    }


    @Override
    public void onBackPressed() {
        if (fragment.onBackPressed()) {

        } else {
            super.onBackPressed();
        }
    }


}
